package oa.lyk.service.Personresource;

import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import oa.dao.DeptDao;
import oa.dao.MsgDao;
import oa.dao.RecordDao;
import oa.pojo.Record;

public class RecordManager {
	public String query(HttpServletRequest request,HttpServletResponse respnse){
		try {
			request.setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		String strCurrentPage = request.getParameter("currPage");
		String name=request.getParameter("username");
		if(name != null ){
			try {
				name = new String(name .getBytes("iso8859-1"),"utf-8");
			} catch (UnsupportedEncodingException e1) {
				e1.printStackTrace();
			}
		}
		int currentPage = 1;
		try{
			currentPage=Integer.parseInt(strCurrentPage);
		}catch (Exception e){
			currentPage=1;
		}
		RecordDao rDao = new RecordDao();
		request.setAttribute("Recordpage", rDao.query(currentPage,name));
		return "queryRecordList";
	}
	
	public String delete(HttpServletRequest request,HttpServletResponse respnse){
		try {
			request.setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		String id = request.getParameter("id");
		int currPage = Integer.parseInt(request.getParameter("currPage"));
		request.setAttribute("currPage", currPage);
		RecordDao rDao = new RecordDao();
		rDao.del(id);
		return "delRecord";
	}
	public String now(HttpServletRequest req,HttpServletResponse res){
		return "now";
	}
	
	public String save(HttpServletRequest request,HttpServletResponse respnse){
		try {
			request.setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		String userid = request.getParameter("userid");
		String username = request.getParameter("username");
		String olddept = request.getParameter("olddept");
		DeptDao dDao = new DeptDao();
		String olddeptid=dDao.queryDeptByName(olddept).getDeptId();
		String newdeptid = request.getParameter("newdept");
		String oldjob = request.getParameter("oldjob");
		String newjob = request.getParameter("newjob");
		String otime = request.getParameter("otime");
		RecordDao rDao = new RecordDao();
		Record r = new Record();
		r.setUserId(userid);
		r.setUserName(username);
		r.setOldDeptId(olddeptid);
		r.setNewDeptId(newdeptid);
		r.setOldJob(oldjob);
		r.setNewJob(newjob);
		r.setOperateDate(otime);
		rDao.save(r);
		MsgDao mDao = new MsgDao();
		mDao.change(userid, newdeptid, newjob);
		return "saveRecord";
	}
}
